﻿using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace XamlingCore.AzureHelper
{
    public class AzureBlobHelper
    {
        //CloudBlob blob;

        CloudStorageAccount cloudStorageAccount;
        CloudBlobClient client;
        CloudBlobContainer container;

        public AzureBlobHelper(string containerName)
        {
            cloudStorageAccount = AzureCommon.GetStorageAccount();
            client = cloudStorageAccount.CreateCloudBlobClient();
            container = client.GetContainerReference(containerName);
        }

        public CloudDrive GetDrive(string fileName)
        {
            getBlob(fileName);
            CloudDrive myCloudDrive = cloudStorageAccount.CreateCloudDrive(
             container
             .GetPageBlobReference(fileName)
             .Uri.ToString());

            return myCloudDrive;
        }

        public string GetUri(string fileName)
        {
            return container
             .GetPageBlobReference(fileName)
             .Uri.ToString();
        }

        public void SetPermission(bool isPrivate)
        {
            container.SetPermissions(new BlobContainerPermissions() { PublicAccess = isPrivate ? BlobContainerPublicAccessType.Off : BlobContainerPublicAccessType.Container });
        }

        private CloudBlob getBlob(string blobName)
        {
            CloudBlob blob = container.GetBlobReference(blobName);
            blob.Container.CreateIfNotExist();
            return blob;
        }

        public void Save(string fileName, byte[] data)
        {
            CloudBlob blob = getBlob(fileName);
            blob.UploadByteArray(data);
        }

        public void Save(string fileName, string text)
        {
            CloudBlob blob = getBlob(fileName);
            blob.UploadText(text);
        }

        public string LoadAsString(string fileName)
        {
            CloudBlob blob = getBlob(fileName);

            try
            {
                return blob.DownloadText();
            }
            catch
            {
                return "";
            }
        }

        public byte[] Load(string fileName)
        {
            CloudBlob blob = getBlob(fileName);
            try
            {
                byte[] data = blob.DownloadByteArray();
                return data;
            }
            catch
            {
                return null;
            };
        }

        public void DeleteAll(string dirName)
        {
            var item = container.GetDirectoryReference(dirName);
            
            foreach(var child in item.ListBlobs())
            {
                if(child is CloudBlobDirectory)
                {
                    var dir = child as CloudBlobDirectory;
                    recurseDeleteDir(dir);
                }
            }
        }

        void recurseDeleteDir(CloudBlobDirectory dir)
        {
            foreach (var child in dir.ListBlobs())
            {
                if(child is CloudBlobDirectory)
                {
                    recurseDeleteDir(child as CloudBlobDirectory);
                }
                else if(child is CloudBlockBlob)
                {
                    var block = child as CloudBlockBlob;
                    block.Delete();
                }
                else
                {
                    throw new Exception();
                }
            }
        }
    }
}
